package com.infore.airsupervise.biz.impl;

import android.text.TextUtils;

import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.CompareE;
import com.infore.airsupervise.bean.PointAnalyzeE;
import com.infore.airsupervise.bean.PointQueryE;
import com.infore.airsupervise.bean.StationExpandE;
import com.infore.airsupervise.bean.StationTreeE;
import com.infore.airsupervise.biz.StatisticsBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * 统计业务接口实现
 * <p>
 * 作者: dingxinlin
 * 创建日期: 2017/3/29 17:49
 */

public class StatisticsBizImpl implements StatisticsBiz {

    /**
     * 站点树
     */
    @Override
    public Call<BaseResponse> getStationTree(final OnServerResponseListener<List<StationTreeE>> listener) {

        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_STATION_TREE, Collections.EMPTY_MAP);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<StationTreeE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, StationTreeE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 点位分析
     */
    @Override
    public Call<BaseResponse> getPointAnalyze(
            int type, String time, String mns, final OnServerResponseListener<List<PointAnalyzeE>> listener) {

        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        // 参数
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("factorType", type);
        parameters.put("time", time);
        if(!TextUtils.isEmpty(mns)){
            parameters.put("stationString", mns);
        }


        // 请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_POINT_ANALYZE, parameters);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<PointAnalyzeE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, PointAnalyzeE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 点位分析查询
     */
    @Override
    public Call<BaseResponse> getPointAnalyzeQuery(
            int type, String startTime, String endTime, String mns,
            final OnServerResponseListener<List<PointQueryE>> listener) {

        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        // 参数
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("dataType", type);
        parameters.put("startTime", startTime);
        parameters.put("endTime", endTime);
        if(!TextUtils.isEmpty(mns)){
            parameters.put("stationString", mns);
        }
        // 请求
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_POINT_ANALYZE_QUERY, parameters);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<PointQueryE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, PointQueryE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取站点树
     * @return
     */
    @Override
    public Call<BaseResponse> sendStationTreeRequest(final OnServerResponseListener<List<StationExpandE>> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }

        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_STATION_TREE, Collections.EMPTY_MAP);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<StationExpandE> beanList = JSONUtil.parseList(
                        response.getData(), Constants.RESPONSE_BEAN_LIST_KEY, StationExpandE.class);
                listener.onSucceed(beanList, response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return call;
    }

    /**
     * 获取热力分析的数据
     * @param mn
     * @param time
     * @param factorType
     * @param listener
     * @return
     */
    @Override
    public Call<BaseResponse> sendHotAnalyzerequest(String mn, String time, int factorType, final OnServerResponseListener<List<PointAnalyzeE>> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }
        Map<String,Object> params = new HashMap<>();
        if(!TextUtils.isEmpty(mn)){
            params.put("mn",mn);
        }
        params.put("time",time);
        params.put("factorType" , factorType);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_HOT_ANALYZE,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
               List<PointAnalyzeE> hotList = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY, PointAnalyzeE.class);
                listener.onSucceed(hotList,response.getMessage());
            }
            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return null;
    }

    @Override
    public Call<BaseResponse> sendConstractRequest(String time, String stationString, int dataType, final OnServerResponseListener<List<CompareE>> listener) {
        // 判断网络是否可用
        if (!HttpUtil.getInstance().isNetworkOk()) {
            listener.onFailed(StatusConst.CODE_CONNECT_FAIL, MsgConst.CONNECT_WRONG, MsgConst.PLEASE_CHECK_NETWORK);
            return null;
        }
        Map<String,Object> params = new HashMap<>();
        if(!TextUtils.isEmpty(stationString)){
            params.put("stationString",stationString);
        }
        params.put("time",time);
        params.put("dataType" , dataType);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_CONSTRACT_ANALYZE,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<CompareE> list = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY, CompareE.class);
                listener.onSucceed(list,response.getMessage());
            }
            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.GET_DATA_FAILED, desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return null;
    }
}
